package com.skl.base.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.skl.base.loadsir.EmptyCallback;
import com.skl.base.loadsir.ErrorCallback;
import com.skl.base.loadsir.LoadingCallback;
import com.skl.base.utils.ToastUtil;
import com.skl.base.viewmodel.IMvvmBaseViewModel;

/**
 * 应用模块: activity
 * 类描述: activity抽象基类
 */
public abstract class MvvmBaseActivity<V extends ViewDataBinding, VM extends IMvvmBaseViewModel> extends AppCompatActivity implements IBaseView {
    protected VM viewModel;
    protected V viewDataBinding;
    protected LoadService mLoadService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();
        performDataBinding();
    }

    private void performDataBinding() {
        viewDataBinding = DataBindingUtil.setContentView(this, getLayout());
        this.viewModel = viewModel == null ? getViewModel() : viewModel;
        if (getBindingVariable() > 0) {
            viewDataBinding.setVariable(getBindingVariable(), viewModel);
        }
        viewDataBinding.executePendingBindings();
    }

    private void initViewModel() {
        viewModel = getViewModel();
        if (viewModel != null) {
            viewModel.attachUI(this);
        }
    }

    /**
     * 注册LoadSor
     * @param view
     */
    public void getLoadSir(View view){
        if (mLoadService !=null) {
            mLoadService = LoadSir.getDefault().register(view, new Callback.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    onRetryBtnClick();
                }
            });
        }
    }
    /**
     * 失败重试,重新加载事件
     */
    protected abstract void onRetryBtnClick();

    /**
     * 获取viewModel
     */
    protected abstract VM getViewModel();

    @LayoutRes
    protected abstract int getLayout();

    /**
     * 获取参数Variable
     */
    protected abstract int getBindingVariable();

    private boolean isShowedContent = false;
    @Override
    public void showContent() {
        if (mLoadService != null) {
            isShowedContent = true;
            mLoadService.showSuccess();
        }
    }

    @Override
    public void showLoading() {
        if (mLoadService != null) {
            mLoadService.showCallback(LoadingCallback.class);
        }
    }

    @Override
    public void showEmpty() {
        if (mLoadService != null) {
            mLoadService.showCallback(EmptyCallback.class);
        }
    }

    @Override
    public void showFail(String message) {
        if (null != mLoadService)
        {
            if (!isShowedContent)
            {
                mLoadService.showCallback(ErrorCallback.class);
            }
            else
            {
                ToastUtil.show(this, message);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (viewModel != null && viewModel.isAttachUI()) {
            viewModel.detachUI();
        }
    }
}
